package com.atguigu.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.atguigu.msm.service.MsmService;
import com.atguigu.msm.utils.MsmUtils;
import com.atguigu.servicebase.Myexception.GuliException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean sendPhoneCode(Map<String, Object> codes, String phoneId) {
        // 得到连接实例
        try {
            Client client = MsmUtils.createClient();
            SendSmsRequest request = new SendSmsRequest();
            request.setSignName("阿里云短信测试")
             .setTemplateCode("SMS_154950909")
              .setPhoneNumbers("15534481057")
              .setTemplateParam(JSONObject.toJSONString(codes));
            RuntimeOptions runtime = new RuntimeOptions();
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(request, runtime);
            log.info("数据为::"+sendSmsResponse.getBody());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"发送短信失败");
        }

    }
}
